﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using System.IO;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Product
{
    public partial class ProductStorageList : System.Web.UI.Page
    {

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            
            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            string sql = " from ShopSkuProduct where Status='1'";

            QueryFilterList filters = new QueryFilterList();
            if (!string.IsNullOrEmpty(Request["productNo"]))
            {
                filters.Add(new StringFilter("ProductNo", "%" + Request["productNo"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["minStorage"]))
            {
                filters.Add(new NumberFilter("Storage", "minStorage", int.Parse(Request["minStorage"]), NumberCompairType.Ge));
            }
            if (!string.IsNullOrEmpty(Request["maxStorage"]))
            {
                filters.Add(new NumberFilter("Storage", "maxStorage", int.Parse(Request["maxStorage"]), NumberCompairType.Le));
            }
            tb_search_minStorage.Text = Request["minStorage"];
            tb_search_maxStorage.Text = Request["maxStorage"];
            foreach (DataFilter filter in filters)
            {
                sql += " and " + filter.GetSection();
            }

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //判断是否导出EXCEL操作
                if (!string.IsNullOrEmpty(Request["exportexcel"]))
                {
                    IQuery query = session.CreateQuery(sql);

                    filters.SetUp(query);

                    IList<ShopSkuProduct> list = query.List<ShopSkuProduct>();
                   

                    StringWriter sw = new StringWriter();
                    sw.WriteLine("产品编号\t产品SKU编号\t产品名称\t剩余库存");
                    foreach (ShopSkuProduct item in list)
                    {
                        sw.WriteLine("'" + item.ProductNo + "\t" + item.ProductSkuCode + "\t" + item.ProductSkuTitle + "\t"
                             + item.Storage.ToString() + "\t");
                    }
                    sw.Close();

                    OutputKit.OutputExcel(Response, sw);
                }
                else
                {

                    IQuery query = session.CreateQuery("select count(*)" + sql);
                    filters.SetUp(query);

                    //设置记录总数
                    _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                    query = session.CreateQuery(sql)
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize);
                    filters.SetUp(query);

                    //设置数据源
                    repeater1.DataSource = query.List<ShopSkuProduct>();


                }
            }

            repeater1.DataBind();
        }
        
        protected string GetPath(object code)
        {
            if (code != null)
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    string path = session.CreateSQLQuery("select thumbnail_img from shop_product_info where product_no=:no")
                        .SetParameter("no", code)
                        .UniqueResult<string>();
                    return FileKit.FirstImgPathThumb(path);
                }
            }
            return "";
        }

        protected object GetId(object code)
        {
            object result = 0;
            if (code != null)
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    result = session.CreateSQLQuery("select id from shop_product_info where product_no=:no")
                        .SetParameter("no", code)
                        .UniqueResult();
                }
            }
            return result;
        }
        
        protected string GetName(object code)
        {
            if (code != null)
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    return session.CreateSQLQuery("select product_name from shop_product_info where product_no=:no")
                        .SetParameter("no", code)
                        .UniqueResult<string>();
                }
            }
            return "";
        }
    }
}